/**
 * The class StackAsArray implements the interface Stack and represents a stack using an array of Object data type
 * @author Michael Fildstein, ID: 309161594        
 * @author Tomer Ben Rachel ID: 021982475       
*/  

public class StackAsArray implements Stack {
	
	protected int top;
  protected Object[] storage;


/**   
* Constructor that receives an integer data type representing the size of the stack and creates a new stack with the length we received    
* @param capacity An integer data type representing the size of the stack
* @throws Runtime exception if the size we have received is incorrect
*/  
public StackAsArray(int capacity) {
    if (capacity <= 0){
        throw new IllegalArgumentException("Stack's capacity must be positive");
       }
    storage = new Object[capacity];
    top = -1;
  }


/**   
* The method push receives an Object data type and inserts it to teh top of the stack   
* @param o An Object data type representing the object to be inserted into the stack
* @throws Runtime exception if there is no more room in the stack
*/  

    public void push(Object o) {
          if (top == storage.length)  
              throw new RuntimeException("Stack's underlying storage is overflow");
          top++;
          storage[top] = o;
    }

/**   
* The method pop returns an Object data type from the top of the stack and removes it
* @throws Runtime exception if the stack is empty
* @return An object data type representing the object that was on the top of the stack
*/  


public Object pop() {
          if (top == -1)
                throw new RuntimeException("Stack is empty");
          int currentTop = top;
          top--;
          return storage[currentTop];
    }


/**   
* The method isEmpty checks to see if the stack is empty or not
* @return true if the stack is empty (the field top is equal to -1), false otherwise
*/  


    public boolean isEmpty() {
          return (top == -1);
    }    
}
